package com.hd.shiro2204.config;

import com.hd.shiro2204.utils.Constants;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfig {
    @Bean
    public Realm customRealm(){
        CustomRealm customRealm = new CustomRealm();
        //指定自定义realm使用的密码匹配器对象
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //设置hash匹配器使用的hash算法的名字
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        hashedCredentialsMatcher.setHashIterations(Constants.HASH_TINES);
        customRealm.setCredentialsMatcher(hashedCredentialsMatcher);

        return customRealm;
    }

    /**
     * 定义url和shiro过滤器之间的对应关系 实现shiro白名单功能
     * @return
     */
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(){
        DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();
        //白名单配置方法，过滤器名字为anon ( anonymous匿名的>
        definition.addPathDefinition("/regist.html","anon");
        definition.addPathDefinition("/user/login","anon");
        definition.addPathDefinition("/login.html","anon");
        definition.addPathDefinition("/user/register","anon");
        definition.addPathDefinition("/error","anon");
        definition.addPathDefinition("/role/get","anon");
        definition.addPathDefinition("/role/get1","anon");
        definition.addPathDefinition("/role/addRole","anon");
        definition.addPathDefinition("/role/addPerms","anon");
        definition.addPathDefinition("/role/addPermsByRole","anon");
        //需要登录后才可以方法的url的配置
        //过滤器的名字为authc
        definition.addPathDefinition("/**","authc");
        return definition;
    }
}
